package com.yueyang.sojourn.server.service.sojourn.sequence;

import com.yueyang.sojourn.server.model.enums.OrderTypeEnum;
import com.yueyang.sojourn.server.model.enums.PayConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * @author Andy.yang
 * @version v1
 * @summary
 * @Copyright (c) 2019, YueYang Group All Rights Reserved.
 * @since 2020/3/20
 */
@Service
public class SojournSequnceManager {
    @Value("${spring.application.name}")
    String appName;
    //订单序列
    String orderSequName = "orderSequ";
    //订单退款序列
    String orderRefundSequName = "orderRefundSequ";

    //分账单序列
    String shareSequName = "shareSequ";

    @Autowired
    SojournSequenceService sojournSequenceService;

    /**
     * 订单号生成器: 商户类型 + 交易类型（退款、付款）+ 序列号
     * @return
     */
    public String nextValueOfOrder(OrderTypeEnum orderType){
        Assert.notNull(orderType,"订单类型不能为空");
        Long nextValue = sojournSequenceService.nextValue(appName,orderSequName);
        return orderType.getCode() + "" + PayConstants.TransactionTypeEnum.PAY.getCode() + "" + nextValue;
    }

    /**
     * 订单退款号生成器： 商户类型 + 交易类型（退款、付款）+ 序列号
     * @return
     */
    public String nextValueOfOrderRefund(OrderTypeEnum orderType){
        Assert.notNull(orderType,"订单类型不能为空");
        Long nextValue = sojournSequenceService.nextValue(appName,orderRefundSequName);
        return orderType.getCode() + "" + PayConstants.TransactionTypeEnum.REFUND.getCode() + "" + nextValue;
    }

    /**
     * 分账单号生成器:  交易类型（退款、付款）+ 序列号
     * @return
     */
    public String nextValueOfShare(){
        Long nextValue = sojournSequenceService.nextValue(appName,shareSequName);
        return PayConstants.TransactionTypeEnum.PAY.getCode() + "" + nextValue;
    }
}
